/*
   Copyright (C) 2009 Xizhi Zhu.  All Rights Reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __p2p_app_h__
#define __p2p_app_h__

#include <vector>
#include "real-app.h"
#include "p2p-tracker.h"

class ConnectionCheckTimer;
struct ChunkInfo;

class P2PApp: public RealApplication
{
    friend class ConnectionCheckTimer;

public:
    P2PApp(Node* node);
    virtual ~P2PApp();

    virtual void recv(ADU* packet, nsaddr_t src);

protected:
    virtual int command(int argc, const char*const* argv);

private:
    P2PTracker* tracker_;

    std::vector<nsaddr_t> server_list_;
    std::vector<nsaddr_t> peer_list_;

    ConnectionCheckTimer* connectionCheckTimer_;

    std::vector<ChunkInfo> chunks_;

    int current_downloading_links_;     // how many nodes I'm downloading from

    // called when ConnectionCheckTimer expires
    void checkConnection();

    // request node list
    void requestNodeList();

    // called when node list received
    void handleNodeListReply(P2PPacket* packet);

    // request chunk list
    void requestChunkList();

    // called when chunk list request received
    void handleChunkListRequest(P2PPacket* packet);

    // called when chunk list received
    void handleChunkListReply(P2PPacket* packet);

    // request chunks
    void requestChunk();

    // called when chunks requested
    void handleChunkRequest(P2PPacket* packet);

    // called when chunks received
    void handleChunkReply(P2PPacket* packet);

    // get the next chunk needed, return -1 if having all chunks
    int getNextNeededChunk();
};

struct ChunkInfo
{
    int available_;     // P2P_CHUNK_AVAILABLE (-1), P2P_CHUNK_NOT_AVAILABLE (-2),
                        // or from which node (0, 1, ...) it's downloading now
    std::vector<nsaddr_t> who_has_;     // who has this chunk
};

class ConnectionCheckTimer: public TimerHandler
{
public:
    ConnectionCheckTimer(P2PApp* app);

    // check the connection speed periodically
    virtual void expire(Event *);

private:
    P2PApp* app_;
};

#endif // __p2p_app_h__

